package com.stylefeng.guns.modular.product.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.modular.product.model.Customer;
import com.stylefeng.guns.modular.product.service.ICustomerReportService;
import com.stylefeng.guns.modular.product.service.ICustomerService;
import com.stylefeng.guns.modular.product.service.IProductFxService;
import com.stylefeng.guns.modular.product.service.ISendMailService;
import com.stylefeng.guns.modular.product.warpper.FxReportWarpper;
import com.stylefeng.guns.modular.product.warpper.FxTotalReportWarpper;
import com.stylefeng.guns.modular.product.warpper.OptionReportWarpper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController extends BaseController {

    private String PREFIX = "/product/report/";

    @Autowired
    private IProductFxService productFxService;

    @Autowired
    private ICustomerReportService customerReportService;

    @Autowired
    private ISendMailService sendMailService;

    @Autowired
    private ICustomerService customerService;

    /**
     * 跳转到客户报表首页
     * @return
     */
    @RequestMapping("")
    public String index(){
        return PREFIX + "customerReport.html";
    }

    /**
     * 根据客户名称查询fx报表数据
     * @param customer
     * @return
     */
    @RequestMapping(value = "/fxReport")
    @ResponseBody
    public Object fxReport(@RequestParam(required = false) String customer){
        if(StringUtils.isNotBlank(customer)){
            List<Map<String, Object>> list = productFxService.getFxReportByCustomer(customer);
            return new FxReportWarpper(list).warp();
        }
        return null;
    }

    /**
     * 根据客户名称查询fx_total报表数据
     * @param customer
     * @return
     */
    @RequestMapping(value = "/fxTotalReport")
    @ResponseBody
    public Object fxTotalReport(@RequestParam(required = false) String customer){
        if(StringUtils.isNotBlank(customer)){
            List<Map<String, Object>> list = productFxService.getFxTotalReportByCustomer(customer);
            return new FxTotalReportWarpper(list).warp();
        }
        return null;
    }

    /**
     * 根据客户名称查询option报表数据
     * @param customer
     * @return
     */
    @RequestMapping(value = "/optionReport")
    @ResponseBody
    public Object optionReport(@RequestParam(required = false) String customer){
        if(StringUtils.isNotBlank(customer)){
            List<Map<String, Object>> list = productFxService.getOptionReportByCustomer(customer);
            OptionReportWarpper optionReportWarpper = new OptionReportWarpper(list);
            //optionReportWarpper.warp();
            return optionReportWarpper.warpTheList();
        }
        return null;
    }


    /**
     * 给客户发送报表邮件
     * @param customer
     * @return
     */
    @RequestMapping("/sendMail")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Tip sendMail(@RequestParam String customer) {

        Customer c = customerService.getByUserName(customer);
        if(null == c)
            return new ErrorTip(500,customer+"客户不存在！");

        String content = customerReportService.getHtmlReportContent(customer);
        try {
            sendMailService.sendHtmlMail(c.getEmail(),"customerReport",content);
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new GunsException(BizExceptionEnum.SERVER_ERROR);
        }
        return SUCCESS_TIP;
    }

}
